package br.ufpe.cin.dsoa.visualvm.persistence;

import br.ufpe.cin.dsoa.visualvm.domain.QoSCategory;
import br.ufpe.cin.dsoa.visualvm.domain.QoSCharacteristic;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;



public class CategoryDAO {

	private Collection<QoSCategory> categories;

	public CategoryDAO(){
		this.categories = new Vector<QoSCategory>();
	}

	public void add(QoSCategory category) {

		this.categories.add(category);
	}

	public boolean remove(String string) {
		return this.categories.remove(new QoSCategory(string));
	}

	public QoSCategory getCategory(String selectedCategory) {

		QoSCategory ret = null;
		Iterator<QoSCategory> it = this.categories.iterator();


		boolean stop = false;
		QoSCategory temp = null;
		QoSCategory comparator = new QoSCategory(selectedCategory);
		while(!stop && it.hasNext()){
			temp = it.next();
			stop = temp.equals(comparator);
		}

		if(stop)
			ret = temp;

		return ret;
	}
	
	public void update(String selectedCategory, QoSCategory newCategory) {
		
		QoSCategory actual = this.getCategory(selectedCategory);
		actual.setCharacteristics(newCategory.getCharacteristics());
		actual.setName(newCategory.getName());
		
	}

	public static void main(String[] args) {
		
		CategoryDAO asd = new CategoryDAO();
		asd.add(new QoSCategory("casa comida"));
		asd.remove("casa comida");
		System.out.println(asd.getCategory("casa comida"));
		
		
	}

	public boolean isPresent(QoSCharacteristic characteristic) {
		
		boolean ret = false;
		Iterator<QoSCategory> it = this.categories.iterator();
		
		while(!ret && it.hasNext() )
			ret = it.next().getCharacteristics().contains(characteristic);
	
		return ret;
	}

	public Collection<QoSCategory> getServicesForCharacteristic(QoSCharacteristic characteristic) {

		Collection<QoSCategory> ret = new Vector<QoSCategory>();
		
		for(QoSCategory cat : this.categories){
			if(cat.getCharacteristics().contains(characteristic))
				ret.add(cat);
		}
			
		return ret;
	}

	public boolean contains(QoSCategory category) {
		return this.categories.contains(category);
	}

    public Collection<QoSCategory> getAllCategories() {
        return this.categories;
    }

	
	
	
}

